package main

import (
	"fmt"
	"sync/atomic"
	"time"
	"unsafe"
)

type S1 struct {
	num1 int32 // 4个字节
	num2 int32 // 4个字节
}

type S2 struct {
	num1 int16 //2 2个字节
	num2 int32 // 4个字节
}

func main() {
	fmt.Println(unsafe.Sizeof(S1{}))
	fmt.Println(unsafe.Sizeof(S2{}))
	fmt.Println(unsafe.Alignof(int16(1))) //内存对齐系数
	c := int32(0)

	for i := 0; i < 1000; i++ {
		go add(&c)
	}
	atomic.CompareAndSwapInt32(&c, 10, 100)
	i := atomic.LoadInt64(3)

	time.Sleep(time.Second)
	fmt.Println(c)
	atomic.CompareAndSwapInt32(&c, 1000, 100)
	fmt.Println(c)
}

func add(p *int32) {
	atomic.AddInt32(p, 1)
}
